package com.ftg.learn;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DeliverCallback;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

/**
 * @author kangjx
 * @createTime 2022/9/21
 */
public class LearnRabbitConsumer {

    public static void main(String[] args) throws IOException {
        ConnectionFactory cf = new ConnectionFactory();
        cf.setHost("127.0.0.1");
        cf.setPort(5673);
        cf.setVirtualHost("dev");
        cf.setUsername("kangjx");
        cf.setPassword("kangjx");
        Connection conn = null;
        try {
            conn = cf.newConnection();
            //频道 我们可以操作的接口
            Channel c = conn.createChannel();

//            queue–队列的名称
//            durable–如果我们要声明一个持久队列，则为true（队列将在服务器重启后继续存在）
//            exclusive–如果我们要声明独占队列（仅限于此连接），则为true
//            autoDelete–如果我们正在声明自动删除队列，则为true（服务器将在不再使用时删除它）
//            arguments–队列的其他属性（构造参数）
            c.queueDeclare("hello1", true, false, false, null);
            c.basicConsume("hello1", true, (consumerTag, delivery) -> {
                System.out.println("111111111111");
                                    String message = new String(delivery.getBody(), "UTF-8");
                                    System.out.println(" [x] Received '" + message + "'");
                                }, consumerTag -> {});
            c.queueDelete("hello1");
        } catch (IOException | TimeoutException e) {
            e.printStackTrace();
        } finally {
            conn.close();
        }
    }

}
